﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Read the data from the profile database
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE TravellerName = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            'Get back records
            oleDbConn.Open()
            'Establish data reader object
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            'Check that there are records
            If dr.HasRows Then
                dr.Read() 'first record
                'fill textboxes
                'Sometimes Access gives an error for no values. So add the Not null:
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub btn_SaveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveProfile.Click

        'For Unit7 Session3 - took out newFileName and SqlString to declare them outside If condition
        Dim newFileName As String = ""
        Dim SqlString As String

        'Check if the user has chosen a new picture
        If fup_Picture.HasFile Then
            'Copied from addhotel since there is saving of an image
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"
            fup_Picture.SaveAs(Request.MapPath("Pictures") & "/" & newFileName)
        End If

        'We need to have 2 different SQL Strings depending on whether the user is uploading a file or not
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
        '1. If user has uploaded a picture
        If fup_Picture.HasFile Then
            'Since we are going to replace, we use Update in SQL
            SqlString = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,Picture=@f4 WHERE TravellerName=@f5"
            '2. No Picture - remove the Picture from SQL String
        Else
            SqlString = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3 WHERE TravellerName=@f5"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        If fup_Picture.HasFile Then cmd.Parameters.AddWithValue("@f4", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'before redirecting to another page, clear all the data from memory
        cmd.Dispose()
        'close the database connection and dispose
        oleDbConn.Dispose()
        'redirect to the profile page - pass the username as a parameter
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)
    End Sub
End Class